package com.eco.utils;

import com.eco.constants.Constants;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.MessageDigestAlgorithms;

/* loaded from: newfishway-1.0.0-SNAPSHOT.jar:BOOT-INF/classes/com/eco/utils/MD5Utils.class */
public class MD5Utils {
    public static String MD5(String input) {
        try {
            MessageDigest md5 = MessageDigest.getInstance(MessageDigestAlgorithms.MD5);
            char[] charArray = input.toCharArray();
            byte[] byteArray = new byte[charArray.length];
            for (int i = 0; i < charArray.length; i++) {
                byteArray[i] = (byte) charArray[i];
            }
            byte[] md5Bytes = md5.digest(byteArray);
            StringBuffer hexValue = new StringBuffer();
            for (byte b : md5Bytes) {
                int val = b & 255;
                if (val < 16) {
                    hexValue.append("0");
                }
                hexValue.append(Integer.toHexString(val));
            }
            return hexValue.toString();
        } catch (NoSuchAlgorithmException e) {
            return "check jdk";
        } catch (Exception e2) {
            e2.printStackTrace();
            return "";
        }
    }

    public static String generate(String password) {
        String password2 = MD5(password + Constants.MD5_SALT);
        char[] cs = new char[48];
        for (int i = 0; i < 48; i += 3) {
            cs[i] = password2.charAt((i / 3) * 2);
            char c = Constants.MD5_SALT.charAt(i / 3);
            cs[i + 1] = c;
            cs[i + 2] = password2.charAt(((i / 3) * 2) + 1);
        }
        return new String(cs);
    }

    public static boolean verify(String password, String md5) {
        char[] cs1 = new char[32];
        char[] cs2 = new char[16];
        for (int i = 0; i < 48; i += 3) {
            cs1[(i / 3) * 2] = md5.charAt(i);
            cs1[((i / 3) * 2) + 1] = md5.charAt(i + 2);
            cs2[i / 3] = md5.charAt(i + 1);
        }
        String salt = new String(cs2);
        return md5Hex(password + salt).equals(new String(cs1));
    }

    private static String md5Hex(String src) {
        try {
            MessageDigest md5 = MessageDigest.getInstance(MessageDigestAlgorithms.MD5);
            byte[] bs = md5.digest(src.getBytes());
            return new String(new Hex().encode(bs));
        } catch (Exception e) {
            return null;
        }
    }
}